package com.mmlc.utils;

import android.util.Log;

import com.androidnetframe.utils.DateUtils;
import com.androidnetframe.utils.LogUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Date;

public class PaaCreator {
    public static JSONObject randomPaa(String order_id,String order_name,String order_price, String receiveUrl,
    String ext_1) {
        String timeStr = DateUtils.changeDateStyle(new Date(), "yyyyMMddHHmmss");
        int price = (int) (Float.parseFloat(order_price) * 100);

        JSONObject paaParams = new JSONObject();
        try {
            paaParams.put("inputCharset", "1");
            paaParams.put("receiveUrl", receiveUrl);
            paaParams.put("version", "v1.0");
            paaParams.put("signType", "1");
            paaParams.put("merchantId", Constants.Union_ACCOUNT);
//            paaParams.put("orderNo", timeStr + "0000");
            paaParams.put("orderNo",order_id);
            paaParams.put("orderAmount", price);
            paaParams.put("orderCurrency", "0");
            paaParams.put("orderDatetime", timeStr);
            paaParams.put("productName", order_name);
            paaParams.put("payType", "0");
            paaParams.put("ext1", ext_1);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        String[] paaParamsArray = {
                "1", "inputCharset",
                receiveUrl, "receiveUrl",
                "v1.0", "version",
                "1", "signType",
                Constants.Union_ACCOUNT, "merchantId",
                order_id, "orderNo",
                String.valueOf(price), "orderAmount",
                "0", "orderCurrency",
                timeStr, "orderDatetime",
                order_name, "productName",
                "0", "payType",
                ext_1,"ext1",
                Constants.Union_KEY, "key",
        };

        String paaStr = "";
        for (int i = 0; i < paaParamsArray.length; i++) {
            paaStr += paaParamsArray[i + 1] + "=" + paaParamsArray[i] + "&";
            i++;
        }
        Log.d("PaaCreator", "PaaCreator " + paaStr.substring(0, paaStr.length() - 1));
        String md5Str = md5(paaStr.substring(0, paaStr.length() - 1));
        Log.d("PaaCreator", "PaaCreator md5Str " + md5Str);
        try {
            paaParams.put("signMsg", md5Str);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        LogUtils.e("PaaCreator", "PayData:" + paaParams.toString());

        return paaParams;
    }

    public static String md5(String string) {
        byte[] hash;
        try {
            hash = MessageDigest.getInstance("MD5").digest(
                    string.getBytes("UTF-8"));
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("Huh, MD5 should be supported?", e);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException("Huh, UTF-8 should be supported?", e);
        }

        return hexString(hash);
    }

    public static final String hexString(byte[] bytes) {
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < bytes.length; i++) {
            buffer.append(hexString(bytes[i]));
        }
        return buffer.toString();
    }

    public static final String hexString(byte byte0) {
        char ac[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
        char ac1[] = new char[2];
        ac1[0] = ac[byte0 >>> 4 & 0xf];
        ac1[1] = ac[byte0 & 0xf];
        String s = new String(ac1);
        return s;
    }

}
